﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OneMake.Model
{
    //全网各品牌金额占比
    public class report_qw_trademark_rate
    {
        public string type { get; set; }

        public Dictionary<DateTime, int> vals { get; set; }

        public Dictionary<DateTime, double> rates { get; set; }

        public report_qw_trademark_rate()
        {
            vals = new Dictionary<DateTime, int>();
            rates = new Dictionary<DateTime, double>();
        }
    }

    partial class ReportHelper
    {
        string[] qw_trademark_rate_type = new string[] { "背心", "风衣", "衬衫", "棉衣", "夹克", "西服", "西服套装", "牛仔裤", "毛衣", "西裤", "休闲裤", "羽绒服", "皮衣", "POLO衫", "T恤" };
        //按月份统计
        public void get_report_qw_trademark_rate(string brand, DateTime month, List<report_qw_trademark_rate> reports)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            db.OneTimeCommandTimeout = int.MaxValue;
            month = month.AddDays(1 - month.Day);
            //按月累计，品牌+类别
            string sql = "select sum(n_amount) from mf_m_ppsczdhq where brand=@0 and category=@1 and stat_datetime=@2";
            foreach (string cate in qw_trademark_rate_type)
            {
                report_qw_trademark_rate selReport = null;
                foreach (report_qw_trademark_rate report in reports)
                {
                    if (cate == report.type)
                    {
                        selReport = report;
                        break;
                    }
                }

                if (selReport == null)
                {
                    selReport = new report_qw_trademark_rate();
                    reports.Add(selReport);
                }

                selReport.type = cate;
                int sum = db.SafeExecuteScalar<int>(sql, brand, cate, month.ToShortDateString());
                selReport.vals.Add(month, sum);
            }
        }

        public List<report_qw_trademark_rate> get_report_qw_trademark_rate(string brand, DateTime beginTime, DateTime endTime)
        {
            System.Diagnostics.Debug.Assert(beginTime <= endTime);
            beginTime = beginTime.AddDays(1 - beginTime.Day);
            endTime = endTime.AddDays(1 - endTime.Day);

            List<report_qw_trademark_rate> reports = new List<report_qw_trademark_rate>();
            DateTime tempTime = beginTime;
            do
            {
                get_report_qw_trademark_rate(brand, tempTime, reports);
                tempTime = tempTime.AddMonths(1);
            }
            while (tempTime <= endTime);

            //计算总计
            report_qw_trademark_rate reportAll = new report_qw_trademark_rate();
            reportAll.type = "--总计--";

            foreach (report_qw_trademark_rate report in reports)
            {
                foreach (KeyValuePair<DateTime, int> val in report.vals)
                {
                    if (!reportAll.vals.ContainsKey(val.Key))
                        reportAll.vals.Add(val.Key, 0);
                    reportAll.vals[val.Key] += val.Value;
                }
            }

            foreach (report_qw_trademark_rate report in reports)
            {
                foreach (KeyValuePair<DateTime, int> val in report.vals)
                {
                    if (!report.rates.ContainsKey(val.Key))
                    {
                        report.rates.Add(val.Key, 0.0);
                    }

                    if (!reportAll.rates.ContainsKey(val.Key))
                    {
                        reportAll.rates.Add(val.Key, 1.0);
                    }

                    int sumAll = reportAll.vals[val.Key];
                    if (sumAll != 0)
                        report.rates[val.Key] = (double)report.vals[val.Key] / sumAll;
                }
            }
            reports.Add(reportAll);

            return reports;
        }

        public List<string> get_all_brands()
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select brand from mf_m_ppsczdhq where brand is not null group by trim(brand)";
            IEnumerable<string> brands = db.Query<string>(sql);
            return brands.ToList<string>();
        }
    }
}
